Introduction

Shiny for R

NHS-R Community

Welcome

What is Shiny?

  • Shiny is a web application programming framework
  • The language of the web (HTML, JavaScript, and CSS) all called from R
  • It requires no knowledge of HTML, JavaScript, or CSS, but if you have that knowledge you can add more of these languages

Examples of Shiny

The Shiny Gallery which includes dashboards as well as parts (widgets) that can be added.

And from healthcare:

Screenshot of the Indices of Deprivation 2019 dashboard from Trafford Data lab

Mental Health Surge Model app

Screenshot of Mental Health Modelling app from The Strategy Unit used to estimate the impacts of COVID-19 on mental health services in England

Patient Feedback text mining app

Screenshot of Patient Text Mining app from Nottinghamshire Healthcare NHS Foundation Trust (via funding from NHS England)

Create an example shiny app

  • Go to File > New Project... > New Directory
  • Select Shiny Application
  • Browse to the location you want the new folder to go to
  • Name your new project

Projects

  • Using projects is a good habit to get into
  • Creating a specific project through the RStudio provides all the relevant documents needed

Let’s create a shiny template app - in Posit Cloud

  • Go to File > New File > Shiny Web App...
  • Or use the short menu below File
  • Name your new project!

Posit Cloud workspaces are all projects!

  • Because the Cloud is a project it doesn’t need creating
  • What we do here you can also do on your own computer

Running the app

Screenshot of the RStudio pane under Tools and Help with the Run app icon and words

Changing the inputs (from the slider) changes the outputs (the chart)

Viewing as a pop-up… or not

You can change whether the app opens in a separate window in RStudio, the RStudio viewer pane, or externally in your web browser. Use whichever works best for you and your computer setup.

Listening?

Before closing the app look at the R Console:

#> Listening on http://127.0.0.1:3827

127.0.0.1 “this computer” and 3827 is a randomly assigned port number.

R is busy!

  • The R prompt isn’t visible
  • The console toolbar has a stop sign icon
  • No other commands can be used until the Shiny app is stopped!
  • To stop click the stop sign, Esc or Ctrl + C in some programs

Developing a Shiny app

The workflow is:

  • Write code
  • Start app
  • Play with app
  • Repeat

In RStudio you don’t need to stop and start just Reload app

Your turn

  • Use File > New File > Shiny Web App...
  • Drag the slider and see the histogram change
  • Change the colour of the histogram fill to “blue”
  • Change the title of the application

End session

Acknowledgements

Mastering Shiny by Hadley Wickham
Beginners course by Chris Beeley aimed at relatively novice R users
Shiny workshop for NHS-R Community
Shiny workshop materials on the NHS-R Community GitHub. Building Web Apps with R Shiny from PsyTeachR